package com.isyscore.provider;

import com.isyscore.entity.SelfUserEntity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: liuxq@isyscore.com
 * @Date: 2021-04-21 15:07:30
 * @Description: token增加 为返回token增加额外信息
 */
@Component
public class CustomTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        SelfUserEntity user = (SelfUserEntity) authentication.getUserAuthentication().getPrincipal();
        Map<String, Object> map = new HashMap<>();
        map.put("username", user.getUsername());
        map.put("client_id", authentication.getOAuth2Request().getClientId());
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(map);
        return accessToken;
    }
}
